package controller;

import enums.SysInfoEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pojo.StuScoreM;
import service.StuScoreMService;
import utils.ResultUtils;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/score")
public class StuScoreMController {

    @Autowired
    StuScoreMService stuScoreMService;

    @RequestMapping(value = "/toadd")
    public String index(){
        return "/WEB-INF/view/grade/add.jsp";
    }

    @RequestMapping(value = "/addscore",method = RequestMethod.POST)
    @ResponseBody
    public String addcourse(@Valid @RequestBody StuScoreM stuScoreM, HttpServletRequest request){
        try{
            stuScoreMService.addScore(stuScoreM);
        }catch (Exception e){
            request.setAttribute("result","错误");
            e.printStackTrace();
            return SysInfoEnum.LOG_STUSCOREM_ERROR.getMessage();
        }


        return SysInfoEnum.LOG_STUSCOREM_SUCCESS.getMessage();
    }

    //跳转edit界面
    @RequestMapping(value = "/index2")
    public String index2(String id,HttpServletRequest request){
        request.setAttribute("id",id);
        return "/WEB-INF/view/grade/edit.jsp";
    }
    @RequestMapping(value = "/getOne" ,method = RequestMethod.POST)
    @ResponseBody
    public StuScoreM getOne(@RequestParam("id") String id){
        return stuScoreMService.getOne(id);
    }
    @RequestMapping(value = "/updateScore",method = RequestMethod.POST)
    @ResponseBody
    public String updatecourse(@RequestBody StuScoreM stuScoreM){
        System.out.println(stuScoreM);
        Integer result = 0;
        try{
           result =  stuScoreMService.updateScore(stuScoreM);
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtils.addResult(result);
        }
        return ResultUtils.addResult(result);
    }

    @RequestMapping(value = "/delete")
    public String deleteCourse(String id){
        stuScoreMService.deleteScore(id);
        return "/demo/score/index4";
    }

    @RequestMapping(value = "/index4")
    public String index4(){
        return "/WEB-INF/view/grade/grade.jsp";
    }

    @RequestMapping(value = "/getScore",method = RequestMethod.GET)
    @ResponseBody
    public List<StuScoreM> getAll(@RequestParam("name")String name){
        System.out.println(name);
        return stuScoreMService.getAll(name);
    }


    @RequestMapping(value = "/getByArray")
    @ResponseBody
    public List<StuScoreM> getByArray(@RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage,
                                 @RequestParam(value = "size",defaultValue = "5") Integer size){
        List<StuScoreM> stuScoreMS =  stuScoreMService.getStuScoreByArray(currentPage, size);
//        map.put("number",userFeedbackDTOPage.getTotalElements());
        return stuScoreMS;
    }

    @RequestMapping(value = "/getNumber")
    @ResponseBody
    public Integer getNumber(){
        List<StuScoreM> stuScoreMS = stuScoreMService.getAll();
        return stuScoreMS.size();
    }
}
